package com.cpp.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cppddd.util.SinaWeiboUtil;

import weibo4j.model.Paging;
import weibo4j.model.User;
import weibo4j.model.UserWapper;
import weibo4j.model.WeiboException;
import weibo4j.org.json.JSONArray;
import weibo4j.org.json.JSONException;
import weibo4j.org.json.JSONObject;

/**
 * Servlet implementation class Friends
 */
public class Friends_Sina extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Friends_Sina() {
        super();
        
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("asdfasdf");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		JSONObject jsonObject = new JSONObject();
		
		int count = 20;//每页显示多少条
		if(request.getParameter("count") != null){
			count =Integer.parseInt(request.getParameter("count"));
		}
		int pageNum = 1;//多少页
		if(request.getParameter("pageNum") != null){
			pageNum = Integer.parseInt(request.getParameter("pageNum"));
		}
		PrintWriter pw = response.getWriter();
		String token =(String) request.getSession().getAttribute("token");
		User user = (User)request.getSession().getAttribute("user");
		Paging page = new Paging();
		page.setCount(count);
		page.setPage(pageNum);
		
		try {
			UserWapper userWapper = new SinaWeiboUtil(token).getFriendsByID(user.getId(), page);
			System.out.println(userWapper.getNextCursor());
			System.out.println(userWapper.getPreviousCursor());
			System.out.println(userWapper.getTotalNumber());
			List<User> users = userWapper.getUsers();
			jsonObject.put("success", "0");
			JSONArray array = new JSONArray();
			for(User u : users){
				Map map = new HashMap();
				map.put("name", u.getName());//名字
				map.put("location", u.getLocation());//地址
				map.put("friendsCount", u.getFriendsCount());//关注数
				map.put("followersCount", u.getFollowersCount());//粉丝数
				map.put("statusesCount", u.getStatusesCount());//微博数
				map.put("description", u.getDescription());//简介
				map.put("followMe", u.isFollowMe());//是否关注我				
				array.put(map);
			}
			jsonObject.put("nextCursor", userWapper.getNextCursor());
			jsonObject.put("previousCursor", userWapper.getPreviousCursor());
			jsonObject.put("totalNumber", userWapper.getTotalNumber());
			jsonObject.put("myfriends", array);
			pw.write(jsonObject.toString());
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		pw.close();
	}

}
